function TreeNode(val) {
    this.val = val
    this.left = null
    this.right = null
}

function buildTree(inorder, postorder) {
    function createTree(inorder, postorder, n) {
        if (n === 0) {
            return null
        }
        let k = 0
        while (postorder[n-1] !== inorder[k]) {
            k += 1
        }
        let node = new TreeNode(inorder[k])
        node.right = createTree(inorder.slice(k + 1, n), postorder.slice(k, n - 1), n - k - 1)
        node.left = createTree(inorder.slice(0, k), postorder.slice(0,k), k)
        return node
    }
    return createTree(inorder, postorder, postorder.length)
}

let postorder = ['D', 'E', 'B', 'F', 'C', 'A']
let inorder = ['D', 'B', 'E', 'A', 'C', 'F']

console.log(buildTree(inorder, postorder));